package com.frogs.ucenter.v1.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.PageTurn;
import com.frogs.framework.util.StringUtil;
import com.frogs.framework.util.date.DateUtil;
import com.frogs.system.util.GoodsEvaluateEnum;
import com.frogs.system.util.UserTypeEnum;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.system.web.exception.ServiceOperationException;
import com.frogs.ucenter.component.EvaluateAsyncHelper;
import com.frogs.ucenter.component.RequestApiHelper;
import com.frogs.ucenter.dao.GoodsEvaluateScoreDao;
import com.frogs.ucenter.dao.GoodsEvaluateTypeLogDao;
import com.frogs.ucenter.dao.TagEvaluateRelationDao;
import com.frogs.ucenter.dao.UserBaseInfoDao;
import com.frogs.ucenter.entity.GoodsEvaluate;
import com.frogs.ucenter.entity.GoodsEvaluateInfo;
import com.frogs.ucenter.entity.GoodsEvaluateTypeLog;
import com.frogs.ucenter.entity.UserLevelInfo;
import com.frogs.ucenter.util.DataCacheKey;
import com.frogs.ucenter.v1.request.GoodsEvaluateBatchMergeRequest;
import com.frogs.ucenter.v1.request.GoodsEvaluateCountRequest;
import com.frogs.ucenter.v1.request.GoodsEvaluateDeleteRequest;
import com.frogs.ucenter.v1.request.GoodsEvaluateIsPraiseRequest;
import com.frogs.ucenter.v1.request.GoodsEvaluateListRequest;
import com.frogs.ucenter.v1.request.GoodsEvaluateMergeRequest;
import com.frogs.ucenter.v1.request.TagGoodsRelationListRequest;
import com.frogs.ucenter.v1.response.AddResponse;
import com.frogs.ucenter.v1.response.DefaultResponse;
import com.frogs.ucenter.v1.response.DeleteResponse;
import com.frogs.ucenter.v1.response.GoodsEvaluateCountResponse;
import com.frogs.ucenter.v1.response.GoodsEvaluateIsPraiseResponse;
import com.frogs.ucenter.v1.response.ListResponse;
import com.frogs.ucenter.v1.response.PageListResponse;
import com.frogs.ucenter.v1.service.GoodsEvaluateService;
import com.frogs.ucenter.v1.util.ApiConstants;

/**
 *
 * @author lijian
 * @version 2014年3月15日 - 下午2:08:13
 */
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service(ApiConstants.VERSION+"goodsEvaluateService")
public class GoodsEvaluateServiceImpl extends GoodsEvaluateBaseServiceImpl implements GoodsEvaluateService {
	
	private static final Logger log = LoggerFactory.getLogger(GoodsEvaluateServiceImpl.class);

    @Resource
    private TagEvaluateRelationDao tagEvaluateRelationDao;
    
    @Resource
    private GoodsEvaluateTypeLogDao goodsEvaluateTypeLogDao;
    
    @Resource
    private UserBaseInfoDao userBaseInfoDao;
    
    @Resource
    private GoodsEvaluateScoreDao goodsEvaluateScoreDao;
    
    @Resource(name="requestApiHelper")
    private RequestApiHelper apiHelper;
    
    @Resource(name="evaluateAsyncHelper")
    private EvaluateAsyncHelper asyncHelper;
    
    /**
     * 添加商品评价
     */
	@Override
    public Response mergeEvaluateInfo(Request request) throws Exception {
		AddResponse<GoodsEvaluate> response = new AddResponse<GoodsEvaluate>();
		try {
			GoodsEvaluateMergeRequest evaluateRequest = (GoodsEvaluateMergeRequest)request;
			int userId = evaluateRequest.getUserId();
			int goodsId = evaluateRequest.getGoodsId();
			int mealId = evaluateRequest.getMealId();
			String tagIds = evaluateRequest.getTagIds();
			int originalGoodsId = evaluateRequest.getOriginalGoodsId();
			int isPharmacistEvaluate = evaluateRequest.getIsPharmacistEvaluate();
			String content = evaluateRequest.getContent();
			String tag = evaluateRequest.getTag();
			String buyTime = evaluateRequest.getBuyTime();
			int applicablePlatformId = evaluateRequest.getApplicablePlatformId();
			int score = evaluateRequest.getScore();
			String orderId = evaluateRequest.getOrderId();
			UserLevelInfo userLevelInfo = getUserLevelInfo(userId);
			if (userLevelInfo != null) {
				Integer levelId = userLevelInfo.getLevelId();
				if (userLevelInfo.getDynamicVipLevelId() != null) {
					if (userLevelInfo.getDynamicVipDeadline().getTime() > new Date().getTime()) {
						levelId = userLevelInfo.getDynamicVipLevelId();
					}
				}
				if (levelId != null) {
					if (UserTypeEnum.PHARMACIST.getType() == levelId || UserTypeEnum.SENIORPHARMACIST.getType() == levelId) {
						isPharmacistEvaluate = GoodsEvaluateEnum.IS_PHARMACIST_EVALUATE_YES.getId();
					}
				}
			}
			GoodsEvaluate goodsEvaluate = mergeSingleEvaluateInfo(userId, originalGoodsId, goodsId,mealId, isPharmacistEvaluate, content, tag, buyTime, applicablePlatformId, tagIds, score,orderId);
	    	response.setInfo(goodsEvaluate);
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
        return response;
    }
	
	/**
	 * 批量商品评价
	 */
	@Override
	public Response batchMergeEvaluateInfo(Request request) throws Exception  {
		DefaultResponse response = new DefaultResponse();
		try {
			GoodsEvaluateBatchMergeRequest batchMergeRequest = (GoodsEvaluateBatchMergeRequest) request;
			int userId = batchMergeRequest.getUserId();
			int isPharmacistEvaluate = batchMergeRequest.getIsPharmacistEvaluate();
			int applicablePlatformId = batchMergeRequest.getApplicablePlatformId();
			String evaluates = batchMergeRequest.getEvaluates();
			JSONArray array = JSONArray.parseArray(evaluates);
			if (array == null) {
				throw new ServiceOperationException("评价数组为空");
			}
			UserLevelInfo userLevelInfo = getUserLevelInfo(userId);
			if (userLevelInfo != null) {
				Integer levelId = userLevelInfo.getLevelId();
				if (userLevelInfo.getDynamicVipLevelId() != null) {
					if (userLevelInfo.getDynamicVipDeadline().getTime() > new Date().getTime()) {
						levelId = userLevelInfo.getDynamicVipLevelId();
					}
				}
				if (levelId != null) {
					if (UserTypeEnum.PHARMACIST.getType() == levelId || UserTypeEnum.SENIORPHARMACIST.getType() == levelId) {
						isPharmacistEvaluate = GoodsEvaluateEnum.IS_PHARMACIST_EVALUATE_YES.getId();
					}
				}
			}
			for (int i = 0; i < array.size(); i++) {
				JSONObject object = array.getJSONObject(i);
				if (object == null) {
					throw new ServiceOperationException("评价为空");
				}
				Integer goodsId = object.getInteger("goodsId") == null ? 0 : object.getInteger("goodsId");
				Integer mealId = object.getInteger("mealId") == null ? 0 : object.getInteger("mealId");
				
				if (goodsId < 1 && mealId < 1) {
					throw new ServiceOperationException("error goodsId or mealId");
				}
				Integer originalGoodsId = object.getInteger("originalGoodsId") == null ? 0 : object.getInteger("originalGoodsId");
				if (originalGoodsId == 0 ) {
					throw new ServiceOperationException("error originalGoodsId");
				}
				String content = StringUtil.fixHtml(object.getString("content"));
				if (StringUtil.isEmpty(content)) {
					throw new ServiceOperationException("error content");
				}
				String orderId = object.getString("orderId");
				if(StringUtil.isEmpty(orderId)){
					orderId = "-";
				}
				
				String tag = StringUtil.fixHtml(object.getString("tag"));
				String buyTime = object.getString("buyTime");
				String tagIds = object.getString("tagIds");
				
				Integer score = object.getInteger("score") == null ? 0 : object.getInteger("score");
				mergeSingleEvaluateInfo(userId, originalGoodsId, goodsId,mealId, isPharmacistEvaluate, content, tag, buyTime, applicablePlatformId, tagIds, score,orderId);
			}
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
        return response;
    }
	
	private GoodsEvaluate mergeSingleEvaluateInfo(int userId, int originalGoodsId, int goodsId,int mealId, int isPharmacistEvaluate, String content, String tag,
			String buyTime, int applicablePlatformId, String tagIds, int score,String orderId) {
		Map<String, Object> params = new HashMap<String, Object>();
		List<Integer> ids = apiHelper.getGoodsSimilarList(goodsId);
		
		if(goodsId > 1)
			params.put("eqGoodsId", goodsId);
		if(mealId > 1)
			params.put("eqMealId", mealId);
		params.put("eqUserId", userId);
		int count = goodsEvaluateDao.selectCountByMap(params);
		GoodsEvaluate goodsEvaluate = new GoodsEvaluate();
		Integer evaluateId = 0;
		if (count > 0) {
			PageList<GoodsEvaluate> list = goodsEvaluateDao.selectByMap(params, false);
			goodsEvaluate = list.get(0);
			updateGoodsEvaluate(userId, originalGoodsId, goodsId,mealId, isPharmacistEvaluate, content, tag, buyTime, applicablePlatformId,orderId, goodsEvaluate);
			goodsEvaluate = goodsEvaluateDao.selectById(evaluateId);
		} else {
			goodsEvaluate = this.insertGoodsEvaluate(userId, originalGoodsId, goodsId, mealId , isPharmacistEvaluate, content, tag, buyTime,orderId, applicablePlatformId);
			evaluateId = goodsEvaluate.getId();
		}
		
		asyncHelper.creatEvaluateTagRelation(tagIds, userId, evaluateId);
		
		asyncHelper.insertGoodsEvaluateScore(userId, goodsId,mealId, score);
		
		asyncHelper.cleanEvaluateCache(userId, ids);
		
		return goodsEvaluate;
	}
	
	private GoodsEvaluate insertGoodsEvaluate(int userId, int originalGoodsId, int goodsId,int mealId, int isPharmacistEvaluate, String content,
			String tag, String buyTime, String orderId , int applicablePlatformId) {
		GoodsEvaluate goodsEvaluate = new GoodsEvaluate();
		goodsEvaluate.setUserId(userId);
		goodsEvaluate.setOriginalGoodsId(originalGoodsId);
		goodsEvaluate.setGoodsId(goodsId);
		goodsEvaluate.setMealId(mealId);
		goodsEvaluate.setIsPharmacistEvaluate(isPharmacistEvaluate);
		goodsEvaluate.setContent(content);
		goodsEvaluate.setTag(tag);
		goodsEvaluate.setBuyTime(DateUtil.parseDate(buyTime, "yyyy-MM-dd"));
		goodsEvaluate.setApplicablePlatformId(applicablePlatformId);
		goodsEvaluate.setStatus(GoodsEvaluateEnum.AUDITED_PASS.getId());
		goodsEvaluate.setOrderId(orderId);
		goodsEvaluateDao.insert(goodsEvaluate);
		// 评价采集日志
		StringBuilder sb = new StringBuilder();
		sb.append(userId).append("`");
		sb.append("评价").append("`");
		sb.append(goodsId).append("`");
		sb.append(mealId).append("`");
		sb.append(goodsEvaluate.getCreateTime()).append("`");
		log.info(sb.toString());
		return goodsEvaluate;
	}
	
	private GoodsEvaluate updateGoodsEvaluate(int userId, int originalGoodsId, int goodsId,int mealId, int isPharmacistEvaluate, String content,
			String tag, String buyTime, int applicablePlatformId, String orderId, GoodsEvaluate goodsEvaluate) {
		goodsEvaluate.setIsPharmacistEvaluate(isPharmacistEvaluate);
		goodsEvaluate.setContent(content);
		if (!StringUtil.isEmpty(tag)) {
			goodsEvaluate.setTag(tag);
		}
		if (!StringUtil.isEmpty(buyTime)) {
			goodsEvaluate.setBuyTime(DateUtil.parseDate(buyTime, "yyyy-MM-dd"));
		}
		goodsEvaluate.setApplicablePlatformId(applicablePlatformId);
		goodsEvaluate.setStatus(GoodsEvaluateEnum.AUDITED_PASS.getId());
		goodsEvaluate.setOrderId(orderId);
		goodsEvaluateDao.update(goodsEvaluate);
		// 评价采集日志
		StringBuilder sb = new StringBuilder();
		sb.append(userId).append("`");
		sb.append("评价").append("`");
		sb.append(goodsId).append("`");
		sb.append(mealId).append("`");
		sb.append(goodsEvaluate.getCreateTime()).append("`");
		log.info(sb.toString());
		return goodsEvaluate;
	}
	
	/**
	 * 删除商品评价
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public Response deleteGoodsEvaluateInfo(Request request) throws Exception {
		DeleteResponse response = new DeleteResponse();
		try {
			GoodsEvaluateDeleteRequest goodsEvaluateDeleteRequest = (GoodsEvaluateDeleteRequest)request;
			int userId = goodsEvaluateDeleteRequest.getUserId();
			int goodsId = goodsEvaluateDeleteRequest.getGoodsId();
			int goodsEvaluateId = goodsEvaluateDeleteRequest.getGoodsEvaluateId();
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("eqId", goodsEvaluateId);
			params.put("eqUserId", userId);
			params.put("status", GoodsEvaluateEnum.DELETED.getId());
			int count = goodsEvaluateDao.updateByMap(params);
			if (count > 0) {
				List<Integer> goodsIds = new ArrayList<Integer>();
				goodsIds.add(goodsId);
				asyncHelper.cleanEvaluateCache(userId,goodsIds );
				response.setCode(ApiResultEnum.SUCCESS.getId());
			}
			else{
				response.setCode(ApiResultEnum.FAIL.getId());
			}
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}
	
	/**
	 * 赞/踩
	 */
	@Override
	public Response isPraise(Request request) throws Exception  {
		GoodsEvaluateIsPraiseResponse response = new GoodsEvaluateIsPraiseResponse();
		try {
			GoodsEvaluateIsPraiseRequest goodsEvaluateIsPraiseRequest = (GoodsEvaluateIsPraiseRequest) request;
			int userId = goodsEvaluateIsPraiseRequest.getUserId();
			int goodsEvaluateId = goodsEvaluateIsPraiseRequest.getGoodsEvaluateId();
			int isPraise = goodsEvaluateIsPraiseRequest.getIsPraise();
			GoodsEvaluate goodsEvaluate = goodsEvaluateDao.selectById(goodsEvaluateId);
			if (goodsEvaluate != null) {
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("eqUserId", userId);
				param.put("eqEvaluateId", goodsEvaluateId);
				int count = goodsEvaluateTypeLogDao.selectCountByMap(param);
				if (count > 0) {
					response.setCode(ApiResultEnum.FAIL.getId());
					response.setTip("一个用户只能操作一次");
					return response;
				}
				Integer receivedSeveral = goodsEvaluate.getReceivedSeveral() == null ? 0 : goodsEvaluate.getReceivedSeveral();
				param.clear();
				param.put("eqId", goodsEvaluateId);
				GoodsEvaluateTypeLog goodsEvaluateTypeLog = new GoodsEvaluateTypeLog();
				goodsEvaluateTypeLog.setUserId(userId);
				goodsEvaluateTypeLog.setEvaluateId(goodsEvaluateId);
				int score = 1;
				UserLevelInfo userLevelInfo = userLevelInfoDao.selectById(userId);
				Integer levelId = userLevelInfo.getLevelId();
				if (levelId == null || levelId == UserTypeEnum.PHARMACIST.getType()) {
					score = 10;
				}
				if (levelId == UserTypeEnum.SENIORPHARMACIST.getType()) {
					score = 20;
				}
				if (isPraise == GoodsEvaluateEnum.PRAISE.getId()) {
					Integer praise = goodsEvaluate.getPraise();
					if (praise == null) {
						praise = 0;
					}
					param.put("praise", praise + score);
					param.put("receivedSeveral", receivedSeveral + score);
					goodsEvaluateTypeLog.setType(GoodsEvaluateEnum.PRAISE.getId());
				}
				if (isPraise == GoodsEvaluateEnum.TRAMPLE.getId()) {
					Integer trample = goodsEvaluate.getTrample();
					if (trample == null) {
						trample = 0;
					}
					param.put("trample", trample + score);
					param.put("receivedSeveral", receivedSeveral - score);
					goodsEvaluateTypeLog.setType(GoodsEvaluateEnum.TRAMPLE.getId());
				}
				goodsEvaluateTypeLogDao.insert(goodsEvaluateTypeLog);
				goodsEvaluateDao.updateByMap(param);
				response.setCode(ApiResultEnum.SUCCESS.getId());
				List<Integer> goodsIds = new ArrayList<Integer>();
				goodsIds.add(goodsEvaluate.getGoodsId());
				asyncHelper.cleanEvaluateCache(userId, goodsIds);
			} else {
				response.setCode(ApiResultEnum.FAIL.getId());
				response.setTip("not found this record");
			}
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}
	
	/**
	 * 获取商品评价列表
	 * 
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Response getGoodsEvaluateList(Request request) throws Exception  {
		PageListResponse<GoodsEvaluateInfo> response = new PageListResponse<GoodsEvaluateInfo>();
		try {
			GoodsEvaluateListRequest evaluateListRequest = (GoodsEvaluateListRequest) request;
			int pageNo = evaluateListRequest.getPageNum();
			int pageSize = evaluateListRequest.getNumPerPage();
			String mapKey = this.buildKey(evaluateListRequest);
			Map<String, Object> cacheEvalute = (Map<String, Object>) cache.get(mapKey);
			List<GoodsEvaluateInfo> evaluateInfos = new ArrayList<GoodsEvaluateInfo>();
			Map<String, Object> obj = new HashMap<String, Object>();
			String key = "_" + pageNo + "_" + pageSize;
			if (cacheEvalute != null) {
				obj = (Map<String, Object>) cacheEvalute.get(key);
				if(obj != null) {
					evaluateInfos = (List<GoodsEvaluateInfo>) obj.get("evaluateInfos");
					response.setPageTurn((PageTurn) obj.get("pageTurn"));
					response.setList(evaluateInfos);
					return response;
				}
			}
			
			PageList<Map<String, Object>> list = this.getList(evaluateListRequest);
			PageList<Map<String, Object>> hotList = this.getHotList(evaluateListRequest);
			PageTurn pageTurn = list.getPageTurn();
			response.setPageTurn(pageTurn);
			if (list != null && list.size() > 0) {
				for (Map<String, Object> object : list) {
					int isHotEvaluate = GoodsEvaluateEnum.HOT_EVALUATE_NO.getId();
					if (hotList != null && hotList.size() > 0) {
						for (int i = 0; i < hotList.size(); i++) {
							Map<String, Object> map = hotList.get(i);
							if (((Integer)object.get("id")).equals((Integer) map.get("id"))) {
								isHotEvaluate = GoodsEvaluateEnum.HOT_EVALUATE_YES.getId();
								break;
							}
						}
					}
					GoodsEvaluateInfo goodsEvaluateInfo = this.setEvaluateInfo(object, isHotEvaluate, 1, 5);
					evaluateInfos.add(goodsEvaluateInfo);
				}
				response.setList(evaluateInfos);
			}
			response.setList(evaluateInfos);
			cacheEvalute = new HashMap<String, Object>();
			obj = new HashMap<String, Object>();
			obj.put("evaluateInfos", evaluateInfos);
			obj.put("pageTurn", pageTurn);
			cacheEvalute.put(key, obj);
			cache.put(mapKey, cacheEvalute, cacheLifeCycle);
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}

	private String buildKey(GoodsEvaluateListRequest evaluateListRequest){
		int userId = evaluateListRequest.getUserId();
		int goodsId = evaluateListRequest.getGoodsId();
		
		int sort = evaluateListRequest.getSort();
		StringBuilder mapKey = new StringBuilder();
		mapKey.append(DataCacheKey.goodsEvaluateMapKey);
		if (userId > 0) {
			mapKey.append("_USER_").append(userId);
		}
		if (goodsId > 0) {
			mapKey.append("_GOODS_").append(goodsId);
		}
		if (sort > 0) {
			mapKey.append("_SORT_").append(sort);
		}
		return mapKey.toString();
	}
	
	private PageList<Map<String, Object>> getList(GoodsEvaluateListRequest evaluateListRequest){
		int userId = evaluateListRequest.getUserId();
		int goodsId = evaluateListRequest.getGoodsId();
		int mealId = evaluateListRequest.getMealId();
		
		int sort = evaluateListRequest.getSort();
		int pageNo = evaluateListRequest.getPageNum();
		int pageSize = evaluateListRequest.getNumPerPage();
		Map<String, Object> params = new HashMap<String, Object>();
		PageList<Map<String, Object>> list = new PageList<Map<String,Object>>();
		if (userId > 0) {
			params.put("eqUserId", userId);
		} else {
			if(goodsId > 0)
				params.put("eqGoodsId", goodsId);
			if(mealId > 0)
				params.put("eqMealId", mealId);
		}
		if (sort == GoodsEvaluateEnum.SORT_CREATE_TIME.getId()) {
			params.put("orderByClause", "a.create_time desc");
		}
		if (sort == GoodsEvaluateEnum.SORT_PRAISE.getId()) {
			params.put("orderByClause", "a.praise desc,a.create_time desc");
		}
		if (sort == GoodsEvaluateEnum.SORT_PHARMACIST.getId()) {
			params.put("orderByClause", "a.is_pharmacist_evaluate desc,a.create_time desc");
		}
		if (sort == GoodsEvaluateEnum.SORT_COMBINATION.getId()) {
			params.put("orderByClause", "a.create_time desc, a.praise desc, a.is_pharmacist_evaluate desc");
		}
		if (sort == GoodsEvaluateEnum.SORT_RECOMMEND.getId()) {
			params.put("eqStatus", GoodsEvaluateEnum.AUDITED_PASS.getId());
			params.put("orderByClause", "a.praise desc, a.is_pharmacist_evaluate desc");
			params.put("eqIsPharmacistEvaluate", GoodsEvaluateEnum.IS_PHARMACIST_EVALUATE_YES.getId());
			PageList<Map<String, Object>> pharmacistRecommendList = goodsEvaluateDao.selectAndUserByMap(params, 1, 2);
			list.addAll(pharmacistRecommendList);
			
			params.put("gtPraise", 0);
			params.put("eqIsPharmacistEvaluate", GoodsEvaluateEnum.IS_PHARMACIST_EVALUATE_NO.getId());
			params.put("orderByClause", "a.praise desc");
			PageList<Map<String, Object>> praiseRecommendList = goodsEvaluateDao.selectAndUserByMap(params, 1, 3);
			list.addAll(praiseRecommendList);
			return list;
		}else{
			params.put("eqStatus", GoodsEvaluateEnum.AUDITED_PASS.getId());
			list = goodsEvaluateDao.selectAndUserByMap(params, pageNo, pageSize);
			return list;
		}
	}
	
	private PageList<Map<String, Object>> getHotList(GoodsEvaluateListRequest evaluateListRequest){
		int userId = evaluateListRequest.getUserId();
		int goodsId = evaluateListRequest.getGoodsId();
		int mealId = evaluateListRequest.getMealId();
		Map<String, Object> params = new HashMap<String, Object>();
		if (userId > 0) {
			params.put("eqUserId", userId);
		} else {
			if(goodsId > 0)
				params.put("eqGoodsId", goodsId);
			if(mealId > 0)
				params.put("eqMealId", mealId);
		}
		params.put("gtPraise", 0);
		params.put("orderByClause", "a.praise desc, a.create_time desc");
		params.put("eqIsPharmacistEvaluate", GoodsEvaluateEnum.IS_PHARMACIST_EVALUATE_NO.getId());
		params.put("eqStatus", GoodsEvaluateEnum.AUDITED_PASS.getId());
		PageList<Map<String, Object>> list = goodsEvaluateDao.selectAndUserByMap(params, 1, 3);
		return list;
	}
	
	/**
	 * 获取商品评价总数
	 */
	@Override
	public Response getEvaluateCount(Request request) throws Exception {
		GoodsEvaluateCountResponse response = new GoodsEvaluateCountResponse();
		try {
			GoodsEvaluateCountRequest goodsEvaluateCountRequest = (GoodsEvaluateCountRequest) request;
			Integer goodsId = goodsEvaluateCountRequest.getGoodsId();
			Integer mealId = goodsEvaluateCountRequest.getMealId();
			Map<String, Object> param = new HashMap<String, Object>();
			if(goodsId > 0)
				param.put("eqGoodsId", goodsId);
			if(mealId > 0)
				param.put("eqMealId", mealId);
			int count = goodsEvaluateDao.selectCountByMap(param);
			response.setCount(count);
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}
}
